<!-- 承运商管理 -->
<template>
  <el-dialog
    title="选择承运商"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @row-click="rowClick"
      @selection-change="selectionChange"
    >
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
      <el-button type="primary" v-noMoreClick @click="binding()"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import { getList } from "@/api/basedata/carriers";
import { demandUpdate } from "@/api/transportation/demandorder";

export default {
  data() {
    return {
      dialogVisible: false,
      id: "",
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectRow: {},
      option: {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          {
            label: "业务伙伴编码",
            prop: "tmId",
            editDisabled: true,
          },
          {
            label: "名称",
            prop: "name",
            editDisabled: true,
            search: true,
          },
          {
            label: "组别",
            prop: "carrierGroup",
            editDisabled: true,
          },
          {
            label: "区域（省份）",
            prop: "addressProvince",
            editDisabled: true,
          },
          {
            label: "城市",
            prop: "addressCity",
            editDisabled: true,
          },
          {
            label: "公司名称",
            prop: "cpmpanyName",
            hide: true,
            showColumn: false,
            rules: [
              {
                required: true,
                message: "请输入公司名称",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
      selection: [], //选择数据
    };
  },
  components: {},
  computed: {},
  watch: {},
  methods: {
    open(id) {
      this.dialogVisible = true;
      this.id = id;
      this.selection = [];
      this.getList(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.getList(this.page, params);
      done();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList(this.page);
    },
    //多选
    selectionChange(list) {
      this.selection = list;
    },
    binding() {
      if (this.selection.length === 0) {
        this.$message({
          message: "请选择要绑定的承运商",
          type: "warning",
        });
        return false;
      }
      if (this.selection.length > 1) {
        this.$message({
          message: "只能选择一个承运商",
          type: "warning",
        });
        return false;
      }
      let row = {};
      row.id = this.id;
      row.fbsId = this.selection[0].id;
      row.fbsName = this.selection[0].name;
      demandUpdate(row).then((res) => {
        this.loading = false;
        if (res.data.code === 200 && res.data.success) {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.dialogVisible = false;
          this.$emit("refresh");
        }
      });
    },
    getList(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.createTime) {
        query = {
          ...query,
          start: query.createTime ? query.createTime[0] : "",
          end: query.createTime ? query.createTime[1] : "",
        };
        delete query.createTime;
      }

      this.loading = true;
      getList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
  },
};
</script>

<style></style>
